﻿using System.Collections.Generic;
using Obsidian.Edm;
using [#BaseNS#];
using [#DalNS#];
using [#ModelNS#];

/*============================
 * @author [#AUTHOR#]
 * @created [#CREATED#]
 * @desc 
* ============================ */

namespace [#LgNS#]
{
	/// <summary>
	/// [#ClsName#] 业务逻辑
	/// </summary>
	public class [#ClsName#]LG : [#ClsBaseLG#]
	{
		public [#ClsName#]LG([#ClsBaseSession#] session) : base(session){}

		#region ADD

		public [#ClsName#]Info Add[#ClsName#]([#ClsName#]Info ei, out string msg)
		{
			[#ClsName#]Info om = null;
			msg = "";

			if (!VerifyAdd(ei, out msg))
			{
				return null;
			}
			//根据需求是否要判断记录已存在
			/*
			[#ClsName#]DA da = new [#ClsName#]DA();
			if (da.ExistsBy(ei.Name.Value, ei.Name))
			{
				msg = "已存在同名记录";
				return null;
			}
			*/

			om = ei;
			//设置空或NULL参数属性的默认值
			[#FOR_DEFAULT_STRING#]
			
			om.Save();
			if (om == null)
			{
				msg = "添加失败";
				return null;
			}
			return om;
		}

		#endregion ADD

		#region UPDATE

		public [#ClsName#]Info Update[#ClsName#]([#ClsName#]Info ei, out string msg)
		{
			[#ClsName#]Info om = null;
			msg = "";

			if (ei.[#IdName#].IsNull)
			{
				msg = "记录ID不能为空";
				return null;
			}
			//根据PrimaryKey判断是否存在记录
			if (!ei.Exists(ei.[#IdName#].Value))
			{
				msg = "该记录不存在";
				return null;
			}

			//设置非空参数属性要修改的值
			om = OModel.GetByPk<[#ClsName#]Info>(ei.[#IdName#].Value);   //通过主键获取记录
			om.ResetAssigned();

			//根据需求是否要判断记录已存在
			/*
			[#ClsName#]DA da = new [#ClsName#]DA();
			if (!ei.Name.IsNullOrWhiteSpace)
			{
				if (!da.ExistsBy(ei.[#IdName#].Value, ei.Name.Value, ei.Name))
				{
					msg = "已存在同名记录";
					return null;
				}
				else
				{
					om.Name.Set(ei.Name.Value);
				}
			}
			*/
			
			//设置非空或非NULL参数属性的值
			[#FOR_UPDATE_STRING#]
			
			if (!om.Update())
			{
				msg = "更新记录失败";
				return null;
			}
			return om;
		}

		#endregion UPDATE

		#region GET

		public [#ClsName#]Info Get[#ClsName#]([#IdSysType#] [#IdAlias#], out string msg)
		{
			[#ClsName#]Info om = null;
			msg = "";

			[#ClsName#]DA da = new [#ClsName#]DA();
			om = OModel.GetByPk<[#ClsName#]Info>([#IdAlias#]);   //通过主键获取记录
			if (om == null)
			{
				msg = "获取记录为空";
				return null;
			}
			return om;
		}
		#endregion GET

		#region LIST

		public List<[#ClsName#]Info> Get[#ClsName#]s([#ClsName#]Qry qry, out ListAttrInfo la, out string msg)
		{
			List<[#ClsName#]Info> list = null;
			la = null;
			msg = "";

			int MIN_PAGESIZE = 10;     //默认分页大小
			int MAX_PAGESIZE = 100;    //最大分页大小
			if (!qry.CheckPagingAttrs(MIN_PAGESIZE, MAX_PAGESIZE, out msg))
			{
				la = null;
				return null;
			}
			[#ClsName#]DA da = new [#ClsName#]DA();
			list = da.SelectList(qry, out la);
			if (list == null)
			{
				msg = "获取列表为空";
				la = null;
				return null;
			}
			return list;
		}

		#endregion LIST

		#region DELETE
		/// <summary>
		/// 删除记录
		/// </summary>
		/// <param name="[#IdAlias#]"></param>
		/// <param name="msg"></param>
		/// <returns></returns>
		public int Delete[#ClsName#]([#IdSysType#] [#IdAlias#], out string msg)
		{
			int code = 0;
			msg = "";

			[#ClsName#]Info om = OModel.GetByPk<[#ClsName#]Info>([#IdAlias#]);
			if (om == null)
			{
				msg = "不存在的记录，无效的删除操作";
				return -1;
			}
			if (!om.Delete())
			{
				msg = "删除失败";
			}
			else
			{
				code = 1;
			}
			return code;
		}

		#endregion DELETE

		#region [验证]

		private bool VerifyAdd([#ClsName#]Info om, out string msg)
		{
			/*
			if (om.Name.IsNullOrWhiteSpace)
			{
				msg = "名称不能为空";
				return false;
			}
			if (om.Status.IsNullOrWhiteSpace) // || !om.CheckStatus(om.Status.Value))
			{
				msg = "状态不能为空或属性错误";
				return false;
			}
			*/

			msg = "";
			return true;
		}

		#endregion [验证]
	}
}